package com.ipharma.application.services.transaccion;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;

import com.ipharma.McdPrs;
import com.ipharma.assignments.Assignment;
import com.ipharma.beans.BeansException;
import com.ipharma.beans.Helper;
import com.ipharma.config.Session;
import com.ipharma.dto.Product;
import com.ipharma.service.TransaccionService;

/**
 * A Class class.
 * <P>
 * @author JorgE.H
 */
public class Transaction extends Object
{
  /**
   * Attributes
   */
  protected Calendar date;     // date of transaction
  protected int type;                   // type of transaction
  protected Hashtable items;            // items of transaction, uses a hashtable to search by code
  protected Session theSess;
  protected TransaccionService dbTrans;
  protected String assignOptName  = "";
  protected Integer assignId      = null;
  protected Integer assignOptId   = null;
  protected Integer patientId     = null;
  protected Assignment theAssign;

  /**
   * Constants
   */
  public static final int EXTRACT       = McdPrs.tsnExtractId;
  public static final int RETURN        = McdPrs.tsnReturnId;
  public static final int RECYCLE       = McdPrs.tsnRecycleId;
  public static final int RECHARGE      = McdPrs.tsnRechargeId;
  public static final String STATUS_OK  = McdPrs.stsOk;
  public static final String STATUS_ER  = McdPrs.stsErr;
  public static final String STATUS_TO  = McdPrs.stsTimeout;

  /**
   * Constructor
   */
  public Transaction( Session pSess )
  {
    try {
      theSess = pSess;
       date    = new GregorianCalendar();
       type    = EXTRACT;
       items   = new Hashtable();
       dbTrans = Helper.getTransaccionService();
   } catch (BeansException e) {
      e.printStackTrace();
   }
  }

  /**
   * Constructor
   * @param pDate date for transaction.
   * @param pType type of transaction (EXTRACTION, RETURN, RECICLED, RECHARGE).
   * @param pItem a Item object to insert into transaction. If null, no item is inserted.
   */
  public Transaction( Session pSess, GregorianCalendar pDate, int pType, Item pItem )
  {
    theSess = pSess;
    date = pDate;
    if ( date == null )
      date = new GregorianCalendar();
    type = pType;
    items = new Hashtable();
    if ( pItem != null )
      items.put( pItem.getCode(), pItem );
  }

  /**
   * Adds a new item into transaction
   * @param pItem a Item object to insert into transaction.
   */
  public void addItem( Item pItem )
  {
    items.put( pItem.getCode(), pItem );
  }

  /**
   * Returns the specified item object
   * @return the Item object
   */
  public Item getItem( String pCode )
  {
    return (Item) items.get( pCode );
  }

  /**
   * Sets the transaction assignemnt
   * @param pAssign the asignment
   */
  public void setAssignment( int pId, int pOptId, String pOptName, int pPatientId )
  {
    this.assignId = new Integer( pId );
    this.assignOptId = (pOptId > 0)? new Integer( pOptId ) : null;
    this.assignOptName = new String( pOptName );
    this.patientId = (pPatientId > 0)? new Integer( pPatientId ) : null;
  }

  /**
   * Sets the transaction assignemnt
   * @param pAssign the asignment
   */
  public void setAssignment( Assignment pAssign )
  {
    theAssign = pAssign;
    this.assignId = new Integer( pAssign.getAstId() );
    this.assignOptId = (pAssign.getAstId() > 0)? new Integer( pAssign.getAstId() ) : null;
    this.assignOptName = new String( pAssign.getAonName() );
    this.patientId = (pAssign.getPttId() > 0)? new Integer( pAssign.getPttId() ) : null;
  }

  /**
   * Ends the trasaction, optionally prints a ticket
   * @return true if alerts where generated, false otherwise
   */
  public boolean end( int pAlerts )
  {
    // inserts transaction into database
    return false;
  }

  /**
   * Prints a ticket for transaction
   */
  public void printTicket( Product pProd )
  {
    Assignment l_ast;
    // gets ticketer
    Ticket l_ticket = (Ticket) theSess.get( McdPrs.aTicketer );
    // prints a ticket
    l_ticket.printHeader();
    l_ticket.printProduct( theAssign, pProd, McdPrs.getOperationName( type - 1 ) );
  }


}

